<?php 

class ObserverDAL {
	private $m_dbConnection="";
	
	public function __construct ($a_dbConnection) {
		$this -> m_dbConnection = $a_dbConnection;
	}
	
	// Get the topmenu and other good to know things about the categories. 
	public function GetMenu ($a_categoryIdName, $a_categoryName, $a_parentIdName, $a_shortDescriptionName){
		$sql = "SELECT category.categoryId, categoryName, parentId, shortDescription 
				FROM category
				ORDER BY category.categoryId";
		$stmt = $this->m_dbConnection -> Prepare($sql);
		
		$stmt->execute();
		$stmt->bind_result($categoryId, $categoryName, $parentId, $shortDescription);
		
		$menuArray = array();
		
		$i = 0;
		
		//Stores all the info in an associative array
		while ($stmt->fetch()) {
			$menuArray[$i] = 
			array($a_categoryIdName => $categoryId, $a_categoryName => $categoryName,
			$a_parentIdName=>$parentId, $a_shortDescriptionName=>$shortDescription);
			$i++;
		}
		return $menuArray;
	}
	
	// Get every post in the database
	public function GetAllPosts (){
		$sql = "SELECT postId, postHeader, postContent, category.categoryId, categoryName, parentId
				FROM post, category
				WHERE post.categoryId = category.categoryId";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt->execute();
		$stmt -> bind_result($postId, $postHeader, $postContent, $categoryId, $categoryName, $parentId);
		
		$allPostsArray = array();
		$i = 0;
		//Fetches all of the posts stores each post as an object post in an array. 
		while ($stmt->fetch()) {
			$allPostsArray[$i] = Post::Create($postId, $postHeader, $postContent, $categoryId, $categoryName, $parentId);
			$i++;	
		}
		return $allPostsArray;
	}
	
	//Gets a single post, return postHeader, postContent and the id that were sent in. 
	public function GetSinglePost ($a_postId, $a_postIdName, $a_postHeaderName, $a_postContentName) {
		$sql = "SELECT postHeader, postContent FROM post WHERE postId=?";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("i", $a_postId);
		$stmt->execute();
		$stmt -> bind_result($postHeader, $postContent);
		
		$postArray = array();
		
		//Fetching the post, storing it in a associative array
		while ($stmt->fetch()) {
			$postArray = array($a_postIdName =>$a_postId, $a_postHeaderName=>$postHeader, $a_postContentName=>$postContent);	
		}
		return $postArray;
	}
	
	//Updates the post with new header and content content. 
	public function UpdatePost($a_header, $a_content, $a_postId){
		$sql = "UPDATE post SET postContent=?,postHeader=? WHERE postId= ?";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param('ssi', $a_content, $a_header, $a_postId);
		
		if($stmt->execute()){
			return true;
		}else {
			false;
		}
	}
	
	//Deletes a post based on postId
	public function DeletePost($a_postId){
		$sql = "DELETE FROM post WHERE postId= ?";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param('i', $a_postId);
		
		if($stmt->execute()){
			return true;
		}else {
			false;
		}
	}
	
	//Makes a new clean post. Yay! 
	public function NewPost ($a_header, $a_content, $a_categoryId){
		$sql = "INSERT INTO `post`(`postHeader`, `postContent`, `categoryId`) VALUES (?, ?, ?)";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param('ssi', $a_header, $a_content, $a_categoryId);
		
		if($stmt->execute()){
			return true;
		}else {
			false;
		}
	}
}
















